/*
	
	raee_fix
	Richard Edden
	
	Last Modified:	February 27, 2002
This program will excise a peak from the spectrum, regenerate a single peak fid, 
leftshift and multiply by a coupling term..

*/

//	Declare some constants and variables

#define PAR1D		0	// 2 dimensional spectrum

int	i,
	iParmode,			// 1D, 2D or 3D spectrum
	iLeftPoint,			// Edges of region to be excised
	iRightPoint,
	iSI,				// SI
	Row,				// Row of 2D to be pulled out.
	Procno1d,			// Process number it is moved to.
	expno2d,
	fidno,			// expno of regenerated fid.
	*iBuffer,
	iD,			//  Number of datapoints in D2
	iTD;


float	f,			//  float version of i
	fphc0,			// phase corrections
	fphc20,			// To select imaginary part at the end (= fphc0 + 90)
	fphc1,
	fOffset,			// 
	fRightPPM,			// Edges of region to be excised ppm
	fLeftPPM,
	fDW,			
	fTime,			// Time in fid
	fJtest,	fW,		// Trial value of J
	fSinpijt,
        fSinWt,
	fCosWt,
	fLB,
	Number;
	
double fDelay,			//D2/DW
	fD2;

const float Pi = 3.1415927;


char	filename1d[PATH_LENGTH],
	filenamefid[PATH_LENGTH];

double dSW;

FILE	*fp1r,			//1d file (2dexpno, procno1d)
	*fq1r;			//fid file (2dexpno+100)

// Begin Program

GETCURDATA;
FETCHPARS("PARMODE",&iParmode);
FETCHPAR("PHC0",&fphc0);
FETCHPAR("PHC1",&fphc1);
FETCHPARS("OFFSET",&fOffset);
FETCHPARS("SI",&iSI);
FETCHPARS("SW",&dSW);
FETCHPARS("D 2",&fD2);
FETCHPAR("DW",&fDW);		//	fetches in microseconds.
//FETCHPAR("TD",&iTD);


	//(void)sprintf(text,"DW: %f ",fDW);
	//STOPMSG(text);

fDW /= 1000000;

if (iParmode != PAR1D ) {STOPMSG( "Not a 1D dataset");
}
/*	Define region to be excised	*/

fLeftPPM=6.02;
fRightPPM=5.86;

//ABS;
VIEWDATA;
(void)sprintf(text, "Note boundaries of region to be excised\nand hit Return.");
Proc_err(2, text);
GETFLOAT("Lefthand edge of region to be excised (ppm):", fLeftPPM);
GETFLOAT("Righthand edge of region to be excised (ppm):", fRightPPM);

iLeftPoint = (int) ((fOffset-fLeftPPM)/dSW*iSI);
iRightPoint = (int) ((fOffset-fRightPPM)/dSW*iSI);

(void)sprintf( filename1d,"%s/data/%s/nmr/%s/%d/pdata/%d/1r",disk,user,name,expno,procno);

// Try to open the file

if ((fp1r=fopen(filename1d,"rb")) == NULL)
{
	(void)sprintf(text,"Cannot read 1r file:\n%s ",filename1d);
	STOPMSG(text);
}

iBuffer=(int *) malloc((size_t)(iSI*sizeof(int)));
if (!iBuffer) STOPMSG("Could not allocate enough memory (iBuffer)");

i = fread(iBuffer,sizeof(int),iSI,fp1r);
fclose(fp1r);
if (iSI != i)	{ STOPMSG("Read Error1:  SI != Number of Points Read"); }


/*	Excise region of interest	*/

for (i=0; i < (iLeftPoint); i++) { iBuffer[i]= 0; }
for (i=iRightPoint-1; i < (iSI); i++) { iBuffer[i]= 0; }

fp1r=fopen(filename1d,"wb");
fwrite(iBuffer,sizeof(int),iSI,fp1r);
fclose(fp1r);

VIEWDATA;

HT;
IFT;

fidno = expno+100;

GETINT("Where do you want to send the simulated spectrum?  ", fidno);

GENFID(fidno);

DATASET(name,fidno, 1, disk, user);

(void)sprintf( filenamefid,"%s/data/%s/nmr/%s/%d/fid",disk,user,name,expno);


if ((fq1r=fopen(filenamefid,"rb")) == NULL)
{
	(void)sprintf(text,"Cannot read fid:\n%s ",filenamefid);
	STOPMSG(text);
}


iBuffer=(int *) malloc((size_t)(iSI*sizeof(int)));
if (!iBuffer) STOPMSG("Could not allocate enough memory (iBuffer)");

iTD=iSI;

i = fread(iBuffer,sizeof(int),iSI,fq1r);
fclose(fq1r);
if (iTD != i)	{ STOPMSG("Read Error2:  SI != Number of Points Read"); }

// This is where the data is left shifted.


fD2 = 0.0501;
GETFLOAT("Leftshift by? ", fD2);
fW=0.0;
GETFLOAT("Frequency shift by? ", fW);
fDelay = fD2/fDW;
iD = (int) (fDelay/2.0);
//Proc_err(DEF_ERR_OPT,"iD=%d,fDelay=%f",iD,fDelay);

for (i=0; i < ((iSI)-2*iD); i=i+2) { 
//Proc_err(DEF_ERR_OPT,"Buff[%d]: %d",i, iBuffer[i]);
iBuffer[i]= iBuffer[i+2*iD]; 
iBuffer[i+1]= iBuffer[i+2*iD+1];}
for (i=((iSI)-2*iD); i < (iSI); i++) { iBuffer[i]= 0; }


//  Now for the J trial part
fJtest=0.0;
GETFLOAT("Enter Trial Value of J in Hz: 0 for no coupling)", fJtest);




if (fJtest != 0.0 ) 
{
for (i=0; i < (iSI); i = i + 2) { 
fSinpijt =  sin(i * fDW * fJtest * Pi);
Number = iBuffer[i];
iBuffer[i] *= fSinpijt; 
iBuffer[i+1] *= fSinpijt; 
}
}


if (fW != 0.0 ) 
{
for (i=0; i < (iSI); i = i + 2) { 
fCosWt=cos(i*fW*fDW*2*Pi);
fSinWt=sin(i*fW*fDW*2*Pi);
Number = iBuffer[i];
iBuffer[i] =(Number*fCosWt-iBuffer[i+1]*fSinWt); 

iBuffer[i+1] =(iBuffer[i+1]*fCosWt+Number*fSinWt); 

}}


fq1r=fopen(filenamefid,"wb");
fwrite(iBuffer,sizeof(int),iTD,fq1r);
fclose(fq1r);

if (fJtest!=0.0) fphc20 = -90.0 ;		// selects imaginary part
else fphc20 = 0.0;
fphc1 = 0.0;

STOREPAR("PHC1",fphc1);
STOREPARS("PHC0",fphc20);
STOREPAR("PHC0",fphc20);
STOREPARS("PHC1",fphc1);

STOREPARS("TD",iSI);
STOREPAR("PARMODE",0);
STOREPARS("PARMODE",0);

fLB = 0.0;
GETFLOAT("Line broadening: ", fLB);
STOREPAR("LB",fLB);
EFP;
VIEWDATA;

free(iBuffer);

QUIT;
